package utils;

import android.os.Handler;
import android.os.Message;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 按钮点击后失效几秒 后才能点击
 * Created by Administrator on 2016/3/11.
 */
public class ButtonSleep {

    /** 睡眠时间（毫秒） */
    private Integer SLEEP_TIME = 1500;
    private static ButtonSleep buttonSleep;

    public static ButtonSleep getIntance(){
        if(buttonSleep == null){
            buttonSleep = new ButtonSleep();
        }
        return buttonSleep;
    }

    public void sleepBtn(final View view){
        view.setEnabled(false);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.obj = view;
                handle.sendMessage(message);
            }
        }, SLEEP_TIME);
    }

    private Handler handle = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if((View)msg.obj != null){
                ((View)msg.obj).setEnabled(true);
            }
        }
    };

}
